package com.botu.springcloudaibabadiscoverycomsumer.controller;


import com.alibaba.cloud.nacos.ribbon.NacosRule;
import com.botu.springcloudaibabadiscoverycomsumer.processor.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: hanzeyao
 * @Date: 2019/12/4 15:53
 * @Description:
 */
@RestController
public class NacosConsumerApp {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EchoService echoService;



    @Value("${spring.application.name}")
    private String appName;

    @GetMapping("/echo/app-name")
    public String echoAppName(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-producer");
        String url = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName);
        System.out.println("请求的url:"+url);
        return restTemplate.getForObject(url,String.class);
    }

    @GetMapping("/echo/fegin")
    public String feginDemo(){
        return echoService.echo();
    }


    @GetMapping("/echo-rest/{str}")
    public String rest(@PathVariable String str) {
        return restTemplate.getForObject("http://nacos-producer/echo/" + str,
                String.class);
    }



}
